package com.bbcare.exportExcel.plat.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.log4j.Logger;


public class ExportUtils {
    private final static Logger logger = Logger.getLogger(ExportUtils.class);

    public static String readJsonFile(final String absolutePath) throws Exception {
        FileInputStream in = null;
        InputStreamReader inReader = null;
        BufferedReader bufReader = null;
        StringBuilder sb = new StringBuilder();
        try {
            in = new FileInputStream(absolutePath);
            inReader = new InputStreamReader(in, "UTF-8");
            bufReader = new BufferedReader(inReader);
            String line = null;
            int i = 1;
            while ((line = bufReader.readLine()) != null) {
                sb.append(line);
                i++;
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            throw ex;
        } finally {
            if (null != bufReader) {
                bufReader.close();
            }
            if (null != inReader) {
                inReader.close();
            }
            if (null != in) {
                in.close();
            }
        }

        return sb.toString();
    }

    /**
     * 根据时间和时间格式，校验是否正确。
     *
     * @param length 校验的长度
     * @param sDate  校验的日期
     * @param format 校验的格式
     * @return
     */
    public static boolean isLegalDate(int length, String sDate, String format) {
        int legalLen = length;
        if ((sDate == null) || (sDate.length() != legalLen)) {
            return false;
        }
        DateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception ex) {
            logger.error(ex.getMessage());
            return false;
        }
    }

    /**
     * 检查srcList中的元素是否有重复的。
     * @param srcList
     * @return key "isDuplicated"是"yes"表示srcList中有重复的字符串。
     *         key "duplicated"可以获取List<String>,里面保存了重复出现的字符串。
     */
    public static Map<String,Object> checkDuplicate(List<String> srcList) {
        Map<String,Object> retMap   = new HashMap<>();
        List<String> duplicatedList = new ArrayList<>();
        retMap.put("duplicated", duplicatedList);
        Map<String,String> checkMap = new HashMap<>();
        for(String srcStr : srcList)   {
            if (!checkMap.containsKey(srcStr))  {
                checkMap.put(srcStr, "");
            } else {
                //duplicated string.
                List<String> dList  = (List<String>)retMap.get("duplicated");
                dList.add(srcStr);
            }
        }

        List<String> dList  = (List<String>)retMap.get("duplicated");
        if (!dList.isEmpty())   {
            retMap.put("isDuplicated", "yes");
        } else {
            retMap.put("isDuplicated", "no");
        }
        return retMap;
    }

}
